﻿/*布线端子检测*/
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Common;
public class ExamWiringPoints : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
    // Use this for initialization
    Text Notice;
    Texture2D TargetCursor;
    AudioClip success;
    AudioClip fail;
    public Exam exam;

    void Awake()
    {

    }

    void Start()
    {
        exam = GameObject.Find("ExamScript").GetComponent<Exam>();
        Notice = GameObject.Find("Notice").GetComponent<Text>();

        //添加声音clip
        success = Resources.Load("Audio/success", typeof(AudioClip)) as AudioClip;
        fail = Resources.Load("audio/fail", typeof(AudioClip)) as AudioClip;
        Debug.Log("wirpoint");
    }

    // Update is called once per frame
    void Update()
    {


    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (exam.wiringIndex < Global.WiringObjectsName.Length)
        {
            if (exam.StartPointName == "")
            {
                exam.StartPointName = this.name;
                exam.StartFly = this.transform.position;
                exam.isFly = 1;
                exam.FlyLine.SetActive(true);
            }
            else if (exam.StartPointName != "" && exam.StartPointName != this.name && exam.EndPointName == "")
            {
                exam.EndPointName = this.name;
                exam.isFly = 0;
                exam.FlyLine.SetActive(false);
                exam.FlyLine.transform.position = new Vector3(0, 0, 0);

            }
            if (exam.StartPointName != "" && exam.EndPointName != "")
            {
                string[] curWiring = new string[4];
                //将当前布线对象存入临时数组
                for (int i = 0; i < Global.WiringObjectsName[exam.wiringIndex].Length; i++)
                {
                    curWiring[i] = Global.WiringObjectsName[exam.wiringIndex][i];
                }

                if (curWiring.Contains(exam.StartPointName) && curWiring.Contains(exam.EndPointName))
                {
                    exam.wiringObjects[exam.wiringIndex][1].SetActive(true);
                    exam.wiringIndex++;
                    AudioSource.PlayClipAtPoint(success, this.transform.position);
                    Notice.text = "太棒了，连接正确！加油呀！";
                }
                else
                {
                    Notice.text = "连接错误，请重试！";
                    Global.Score--;
                    Global.WirlingErrorNum++;
                    AudioSource.PlayClipAtPoint(fail, this.transform.position);
                }
                exam.StartPointName = "";
                exam.EndPointName = "";
                exam.isFly = 0;
            }

            //如果布线完成则直接返回
            if (exam.wiringIndex >= Global.WiringObjectsName.Length)
            {
                if (exam.cables[0])
                {
                    exam.cables[0].SetActive(true);
                }
                if (exam.cables[1])
                {
                    exam.cables[1].SetActive(true);
                }

                string report = "";
                string level = "";
                AudioClip gameover;
                if (Global.Score >= 95)
                {
                    level = "优秀";

                }
                else if (Global.Score >= 85)
                {
                    level = "良好";
                }
                else if (Global.Score >= 60)
                {
                    level = "及格";
                }
                else
                {
                    level = "不及格";
                }

                report = "操作考核成绩：<color=red>" + Global.Score + "</color>\n　评定等级：<color=red>" + level + "</color>";
                if (Global.Score < 60)
                {
                    report = report + "\n很遗憾，考核未通过，继续努力！";
                    gameover = Resources.Load("Audio/lose", typeof(AudioClip)) as AudioClip;
                }
                else if (Global.Score < 90 && Global.Score >= 60)
                {
                    report = report + "\n恭喜你，考核通过，希望你做的更完美！";
                    gameover = Resources.Load("Audio/celebrate", typeof(AudioClip)) as AudioClip;

                }
                else
                {
                    report = report + "\n太棒了，非常好，不要骄傲哦！";
                    gameover = Resources.Load("Audio/celebrate", typeof(AudioClip)) as AudioClip;
                }

                AudioSource.PlayClipAtPoint(gameover, Vector3.zero);

                MessageBox.Show("考核成绩", report, "重新考核", "重新练习");
                MessageBox.confim = () =>
                {
                    GameObject.Find("ExamScript").GetComponent<Exam>().BtnAssemble();
                    Debug.Log("重新考核");
                };
                MessageBox.cancel = () => {
                    SceneManager.LoadScene("U3_04_Assembly");

                };

                //原理图提示闪烁停止
                Destroy(exam.wiringObjects[Global.WiringObjectsName.Length - 1][0].GetComponent<UITwinking>());
                exam.wiringObjects[Global.WiringObjectsName.Length - 1][0].GetComponent<CanvasGroup>().alpha = 1;
                //移除连线完成的导线的原理图提示
               // Destroy(exam.wiringObjects[Global.WiringObjectsName.Length - 1][0].GetComponent<WiringHelp>());
            }

        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        //把鼠标指针改为ClickedCursorImg
        TargetCursor = Resources.Load("Cursor/TargetCursor1", typeof(Texture2D)) as Texture2D;
        Cursor.SetCursor(TargetCursor, Vector2.zero, CursorMode.Auto);
        Debug.Log(this.name);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        // 重置鼠标指针图标
        Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
        Debug.Log("mouseout");
    }
}
